package test17.homework;

import java.util.Scanner;

public class Homework01 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();


    }
}

//1)在main方法中启动两个线程
// (2)第1个线程循环随机打印100以内的整数
class A extends Thread{
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        while (flag){
            System.out.println((int)(Math.random() * 100 + 1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
        }
        System.out.println("a线程退出");
    }
}
// (3)直到第2个线程从键盘读取了"Q"命令.
class B extends Thread{
    private Scanner scanner = new Scanner(System.in);
    private A a;

    public B(A a) {
        this.a = a;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("请输入你指令(Q)表示退出:");
            char key = scanner.next().toUpperCase().charAt(0);
            if(key == 'Q'){
                a.setFlag(false);
                System.out.println("B线程退出");
                break;
            }
        }
    }
}
